package com.sisi.web;

import entity.Teacher810;
import util.JavaUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/Hihi")//在网页地址后显示这些操作
public class Hihi extends HttpServlet {//必须继承HttpServlet


    @Override   //重写service方法
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");

        System.out.println("hihi");
        String method = req.getParameter("method");
        if (method == null || method.equals("")){
            method = "selectALl";
        }
        switch (method) {
            case "selectALl" :
                selectALl(req,resp);
                break;
            case "deleteById" :
                deleteById(req,resp);
                break;
            case "insert" :
                insert(req,resp);
                break;
        }


    }



    private void selectALl(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("hihi");
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        ArrayList<Teacher810> arrayList = new ArrayList<>();

        try {
            connection = JavaUtil.getConnection();
            String str = "SELECT id,name,age FROM teacher810";
            statement = connection.prepareStatement(str);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");//或得列
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                Teacher810 t810 = new Teacher810(id,name,age);
                arrayList.add(t810);
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JavaUtil.close(connection, statement ,resultSet);
        }

        req.setAttribute("Array",arrayList);//将list数据放到一块内存区域中
        // 跳转到jsp页面进行展示，dispatcher分发，forward转发
        req.getRequestDispatcher("/teacher810_list.jsp").forward(req,resp);
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");//获取浏览器上的id的值

        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JavaUtil.getConnection();

            String sql = "DELETE FROM teacher810 WHERE id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,Integer.parseInt(id));//类里的parseInt方法
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count:" + count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JavaUtil.close(connection, statement, null);
        }
        //删除完之后返回查找所有的servlet，展示最新的，第一个是
        //resp是服务器返回浏览页，req是从浏览器得到的数据返回服务器
        //重定向，让浏览器发送这个请求
        resp.sendRedirect(req.getContextPath() + "/Hihi?method=selectALl");

    }

    private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 这些也是传递的参数
        //  name: zhangsan
        //  age: 23
        //  gender: 男
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JavaUtil.getConnection();
            String sql = "INSERT INTO teacher810(name,age) VALUES(?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, name);
            statement.setInt(2, Integer.parseInt(age));
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JavaUtil.close(connection, statement, null);
        }

        resp.sendRedirect(req.getContextPath() + "/Hihi?method=selectALl");
    }


}
